package com.javasm.adminapi.Tspu.contorller;

import com.github.pagehelper.PageInfo;
import com.javasm.adminapi.common.R;
import com.javasm.adminapi.module.product.entity.TBrand;
import com.javasm.adminapi.module.product.service.TBrandService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className: TBarndController
 * @description:
 * @author: cx
 * @date: 2025/10/15 16:44
 * @version: 0.1
 * @since: jdk17
 */
@RestController
@RequestMapping("/tBrand")
public class TBarndController {
    @Resource
    TBrandService tBrandService;
    //获取所有品牌id name
    @GetMapping("/list")
    public R list(){
        List<TBrand> list = tBrandService.listall1();
        Map<Integer,String> tBrandmap = new HashMap<>();
        list.forEach(tBrand -> {
            tBrandmap.put(tBrand.getId(),tBrand.getBrandName());
        });
        return R.ok(tBrandmap);
    }

    //获取所有品牌
    @GetMapping("/listAll")
    public R listAll(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                     @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                     @RequestParam(required = false)String brandName,
                     @RequestParam(required = false)Integer isEnabled){
        PageInfo<TBrand> list = tBrandService.listByPage(pageNum, pageSize, brandName, isEnabled);
        return R.ok(list);
    }

    //添加品牌
    @PostMapping("/add")
    public R add(@RequestBody TBrand tBrand){
        boolean save = tBrandService.addTBrand(tBrand);
        return R.ok();
    }

    @PutMapping("/update")
    public R update(@RequestBody TBrand tBrand){
        boolean update = tBrandService.updateByIdinfo(tBrand);
        return R.ok();
    }
    //批量删除品牌
    @PostMapping("/delete")
    public R delete(@RequestBody List<Integer> ids){
        boolean b = tBrandService.removeByIdsInfo(ids);
        return R.ok();
    }

}
